<?php

namespace App\Http\Controllers\Admin;

use App\Http\Config\ErrorCode;
use App\Models\Admin\Articles;
use Illuminate\Http\Request;


class BrowsingController extends BaseController {

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /* 使用帮助 */
	public function help()
	{
	    $articleModel = new Articles();
	    $params = array(
            array('read_power','<',2),
            array('id','=',1)
        );
	    $data = $articleModel->getArticleByCustome($params,'single');
        $this->returnData['data'] = $data ?: [];
        return response()->json($this->returnData);
	}

	/* 关于我们 */
	public function about()
	{
        $articleModel = new Articles();
        $params = array(
            array('read_power','<',2),
            array('id','=',2)
        );
        $data = $articleModel->getArticleByCustome($params,'single');
        $this->returnData['data'] = $data ?: [];
        return response()->json($this->returnData);
	}
	
	/* 日志 */
    public function log(Request $request)
    {
        if (!$request->ajax()) {
            $this->returnData = ErrorCode::$admin_enum['request_error'];
            return response()->json($this->returnData);
        }
        $page_no = $request->post('page_no', 1);
        $page_size = $request->post('page_size', 10);
        $searchFilter = array(
            'sortName' => "created_at",                                                  //排序列名
            'sortOrder' => "desc",                                               //排序（desc，asc）
            'pageSize' => $page_size,                                                                                   //一页显示的条数
            'start' => ($page_no-1) * $page_size,                                                                       //开始位置
            'typeid' => 3,
            'searchKey' => '',
            'is_display' => 0,
            'read_power' => 2
        );
        $articleModel = new Articles();
        $res = $articleModel->getCustomArticlesWithFilter($searchFilter);
        $data = [];
        foreach ($res["rows"] as $v){
            $month = date("Y-m", strtotime($v['created_at']));
            $data[$month] = $v;
        }
        $res["rows"] = array_sort($data);
        $this->returnData['data'] = $data;
        return response()->json($this->returnData);
    }

	/* 加载外部新闻列表 */
    public function getDataList(Request $request)
    {
        if (!$request->ajax()) {
            $this->returnData = ErrorCode::$admin_enum['request_error'];
            return response()->json($this->returnData);
        }
        $page_no = $request->post('page_no', 1);
        $page_size = $request->post('page_size', 10);
        $searchFilter = array(
            'sortName' => $request->post('sortName','created_at'),                                                  //排序列名
            'sortOrder' => $request->post('sortOrder','desc'),                                               //排序（desc，asc）
            'pageSize' => $page_size,                                                                                   //一页显示的条数
            'start' => ($page_no-1) * $page_size,                                                                       //开始位置
            'searchKey' => trim($request->post('search','')),                                               //搜索条件
            'typeid' => 1,
            'is_display' => 0,
            'read_power' => 2
        );
        $articleModel = new Articles();
        $res = $articleModel->getCustomArticlesWithFilter($searchFilter);
        $this->returnData['data'] = $res;
        return response()->json($this->returnData);
    }

    /* 查看新闻详情 */
    public function view($id)
    {
        $articleModel = new Articles();
        $data = $articleModel->getArticlesByID((int)$id);
        $this->returnData['data'] = $data ?: [];
        return response()->json($this->returnData);
    }
}